package org.jeecg.modules.quartz.job;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.dingtalk.DingMessageUtils;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.message.service.ISysMessageTemplateService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.finer.erp.jeecg.bas.entity.BasMaterial;
import io.finer.erp.jeecg.bas.entity.ZrkPushSet;
import io.finer.erp.jeecg.bas.entity.ZrkQzOrder;
import io.finer.erp.jeecg.bas.entity.ZzDevice;
import io.finer.erp.jeecg.bas.entity.ZzDeviceType;
import io.finer.erp.jeecg.bas.service.IBasMaterialService;
import io.finer.erp.jeecg.bas.service.IZrkPushSetService;
import io.finer.erp.jeecg.bas.service.IZrkQzOrderService;
import io.finer.erp.jeecg.bas.service.IZzDeviceService;
import io.finer.erp.jeecg.bas.service.IZzDeviceTypeService;
/**
 * 
* @ClassName: ZzShoporderOvertimeJob 
* @Description: 工单安排到机器后的检测交货时间是否要超时 
* @author heckerstone heckerstone@163.com
* @date Oct 14, 2020 1:34:17 PM 
*
 */
public class ZzShoporderOvertimeJob implements Job {
	
	/**注入切张/分切单服务**/
	@Autowired
	private IZrkQzOrderService zrkQzOrderService;
	
	@Autowired
	private IZrkPushSetService zrkPushSetService;
	@Autowired
	private ISysMessageTemplateService sysMessageTemplateService;
	@Autowired
	private ISysBaseAPI sysBaseApi;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IBasMaterialService basmaterialService;
	@Autowired
	private IZzDeviceService zzDeviceService;
	@Autowired
	private IZzDeviceTypeService zzDeviceTypeService;
	

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
        //轮询判断切张分切单是否要超时
		QueryWrapper<ZrkQzOrder> queryWrapper=new QueryWrapper<ZrkQzOrder>();
		//查询条件(查询未完成的工序)
		queryWrapper.ne("is_finish", "1");
		List<ZrkQzOrder> zqoList=zrkQzOrderService.list(queryWrapper);
		if(null!=zqoList&&zqoList.size()>0) {
			//遍历切张/分切单得到单据信息
			for(ZrkQzOrder zqo:zqoList) {
				//判断每张单据是否要超时
				Date deliverTime=zqo.getWoDeliverytime();//到货时间
				String str=DateUtils.getDatePoor(deliverTime,new Date());
				String [] t=str.split(",");
				int day=Integer.parseInt(t[0]);
				int hour=Integer.parseInt(t[1]);
				//天数小于3天或者天数大于3天但是交货时间小于2小时
				if(!((day>3||day==3)&&(hour>2||hour==2))||day<0||hour<0) {
					//查询短信模版及提醒人信息
					Collection<ZrkPushSet> col=zrkPushSetService.list();
					if(null!=col&&col.size()>0) {
						Iterator<ZrkPushSet> ite=col.iterator();
						while(ite.hasNext()) {
							ZrkPushSet zps=ite.next();
							//库存预警消息类型
							if(zps.getMessageTpe().equals("2")) {
								//生产工单信息需要发送钉钉消息提醒任务分配，查询短信模版
								SysMessageTemplate sysMessage=sysMessageTemplateService.getById(zps.getMessageId());
								//替换模版变量
								String messageContent=sysMessage.getTemplateContent();
								//查询物料
								BasMaterial material=basmaterialService.getById(zqo.getMaterialId());
								//查询设备信息
								ZzDevice device=zzDeviceService.getById(zqo.getMachineId());
								//工序类型查询
								ZzDeviceType deviceType=zzDeviceTypeService.getById(zqo.getProcessId());
								//发送消息时内容带着<p></p>,此处需要替换掉
								messageContent=messageContent.replace("${id}", zqo.getId()).replace("${material_name}", material.getName()).replace("${machine_name}", device.getDeviceName()).replace("${gx}",deviceType.getDeviceTpyeName()).replace("<p>", "").replace("</p>", "");;
								//查询提醒人钉钉ID
								SysUser sysUser=sysUserService.getById(zps.getUserId());
								boolean flag=DingMessageUtils.sendDingMessage(sysUser.getThirdId(), messageContent);
								if(flag) {
									//发送消息成功后,将该任务单状态设置为已推送,防止多次推送（统一消息发送多次在钉钉接口处理的时候会提示143106，该错误码代表发送消息重复，143105:表示企业自建应用每日推送给用户的消息超过上限）
									//发送系统消息
									sysBaseApi.sendSysAnnouncement("admin", sysUser.getUsername(),sysMessage.getTemplateName(), messageContent);
								}
							}
						}
					}
				}
			}
		}
	}

}
